package org.gwc.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.gwc.mapper.ScheduleMapper;
import org.gwc.pojo.Schedule;
import org.gwc.service.ScheduleService;
import org.gwc.utils.PageBean;
import org.gwc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;
    @Override
    public R page(int pageSize, int currentPage) {
        // 分页
        PageHelper.startPage(currentPage,pageSize); // sql limit x,y; 该插件会自动做算法 limit (currentPage-1)*pageSize,pageSize
        // 查询
        List<Schedule> scheduleList = scheduleMapper.querryList();
        // 分页数据装配,在全量数据的基础上，按照分页信息，提取目标数据
        PageInfo<Schedule> info = new PageInfo<>(scheduleList);
        // 装配PageBean
        PageBean<Schedule> pageBean = new PageBean<>(currentPage, pageSize, info.getTotal(), info.getList());
        // 装配R
        R r = R.ok(pageBean);
        return r;
    }

    @Override
    public R deleteById(Integer id) {
        int rows = scheduleMapper.deleteById(id);
        if (rows > 0){
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R insert(Schedule schedule) {
        int rows = scheduleMapper.insert(schedule);
        if (rows>0){
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R update(Schedule schedule) {
        if (schedule.getId() == null){
            return R.fail("id is null");
        }
        int rows = scheduleMapper.update(schedule);
        if (rows > 0){
            return R.ok(null);
        }
        return R.fail(null);
    }
}
